package com.example.testdemo.servlet;

import com.example.testdemo.pojo.Book;
import com.example.testdemo.service.bookService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/opera")
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if ("1".equals(request.getParameter("op"))){
            try {
                List<Book> list = bookService.getallBooks();
                request.getSession().setAttribute("list",list);
                response.sendRedirect("list.jsp");
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        if ("2".equals(request.getParameter("op"))){
            String isbn = request.getParameter("isbn");
            try {
                Book book = bookService.query(isbn);
                if (book == null){
                    pw.println("no exist");
                    pw.flush();
                }else{
                    pw.print(book.getIsbn()+"<br>");
                    pw.print(book.getBname()+"<br>");
                    pw.print(book.getBauther()+"<br>");
                    pw.print(book.getBnumber()+"<br>");
                    pw.print(book.getPrice());
                    pw.flush();
                }

            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        else if ("3".equals(request.getParameter("op"))){
            String isbn = request.getParameter("isbn");
            String bname = request.getParameter("bname");
            String bauther = request.getParameter("bauther");
            String bnumber = request.getParameter("bnumber");
            String price = request.getParameter("price");
            try {
                int isadd = bookService.add(isbn,bname,bauther,bnumber,price);
                if (isadd == 1){
                    pw.print("add success");
                    pw.flush();
                }else {
                    pw.print("add fail");
                    pw.flush();
                }
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        else if ("5".equals(request.getParameter("op"))){
            String isbn = request.getParameter("isbn");
            int i = 0;
            try {
                i = bookService.deletebook(isbn);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (i != 1){
                pw.print("delete fail");
                pw.flush();
            }else{
                pw.print("delete complete");
                pw.flush();
            }
        }
    }
}
